﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace GreenPark
{
    public class Cihaz
    {
        public int No { get; set; }
        public string Tanim { get; set; }
        public string IpAdresi { get; set; }
        public bool KayitKapisi { get; set; }
        public override string ToString()
        {
            return No.ToString();
        }
        public byte Tip { get; set; }
    }

    public enum KayitDurumu
    {
        Eklenecek,
        Eklendi,
        Silindi,
        Silinecek
    }

    [Serializable]
    public class RecordInfo
    {
        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }
        public string EmployeeCode
        {
            get { return personID; }
            set { personID = value; }
        }
        public DateTime RcdTime
        {
            get { return datetime; }
            set { datetime = value; }
        }

        public byte[] ImageData
        {
            get { return photo; }
            set { photo = value; }
        }
        public string DevID
        {
            get { return deviceID; }
            set { deviceID = value; }
        }

        public string EmployeeName
        {
            get { return personName; }
            set { personName = value; }
        }

        private string guid = null;
        private string personID = null;
        private string personName = null;
        private DateTime datetime = new DateTime();
        private byte[] photo = null;
        private string deviceID = null;



    }

    public class Users
    {
        public string ID { get; set; }
        public string Adi { get; set; }
        public KayitDurumu Chz1 { get; set; }
        public KayitDurumu Chz2 { get; set; }
        public KayitDurumu Chz3 { get; set; }
        public KayitDurumu Chz4 { get; set; }

        public override bool Equals(object obj)
        {
            if (this.ID != ((Users)obj).ID)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool AktifMi()
        {
            if (Chz1 == KayitDurumu.Eklendi || Chz2 == KayitDurumu.Eklendi || Chz3 == KayitDurumu.Eklendi || Chz4 == KayitDurumu.Eklendi)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public class BgProcess
    {

        string RemoveTurkishCharacter(string deger)
        {
            return deger.ToUpper().Replace('İ', 'I').Replace('Ö', 'O').Replace('Ü', 'U').Replace('Ş', 'S').Replace('Ğ', 'G').Replace('Ç', 'C');
        }

        public bool CheckContract(string userID)
        {
            bool sonuc = false;
            string conn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(conn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT  count(*)  FROM  Contract  WHERE        (ActiveOn <= GETDATE()) AND (ExpiresOn >= GETDATE()) AND (New_userid = @id)", cn))
                {
                    cmd.Parameters.Add("@id", System.Data.SqlDbType.NVarChar, 50).Value = "pnd-" + userID;
                    SqlDataReader rd = cmd.ExecuteReader();
                    if (rd.HasRows)
                    {
                        rd.Read();
                        if (rd.GetInt32(0) > 0)
                        {
                            sonuc = true;
                        }
                    }
                }
                cn.Close();
            }
            return sonuc;
        }

        public void InsertActivity(int id, string ip)
        {
            string conn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(conn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Action  (ID,GateID, Date, ContactID, Type, InportCrm) VALUES        (@guid,@kapi, GETDATE(), @id, N'OK', 0)", cn))
                {
                    cmd.Parameters.Add("@guid", System.Data.SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
                    cmd.Parameters.Add("@kapi", System.Data.SqlDbType.Int).Value = CihazNo(ip);
                    cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
                    cmd.ExecuteNonQuery();
                }
                cn.Close();
            }
        }

        private int CihazNo(string ip)
        {
            int no = 0;
            foreach (Cihaz item in Program.Db.Cihazlar)
            {
                if (item.IpAdresi == ip.Trim())
                {
                    no = item.No;
                }
            }
            return no;
        }


        public DataTable EmployeFind(string filtre)
        {
            DataTable dt = new DataTable();
            string conn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(conn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("select * from (SELECT        New_userid [Üye No], FullName as [Üye Adı ve Soyadı], case when  (ActiveOn <= GETDATE()) AND (ExpiresOn >= GETDATE()) then 'Aktif' else 'Pasif' end as Durumu  FROM            Contract  WHERE  (New_userid like 'pnd-%') and ((New_userid LIKE @filtre) OR  (FullName LIKE @filtre))) as tbl group by [Üye No],[Üye Adı ve Soyadı],Durumu", cn))
                {
                    cmd.Parameters.Add("@filtre", SqlDbType.NVarChar, 50).Value = "%" + filtre + "%";
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                    }
                }
                cn.Close();
            }
            return dt;
        }

        public void ImportDb()
        {
            List<Users> aktifUyeler = new List<Users>();
            string conn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(conn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT        New_userid, FullName, ActiveOn, ExpiresOn  FROM            Contract  WHERE        (ActiveOn <= GETDATE()) AND (ExpiresOn >= GETDATE()) AND (New_userid <> N'')", cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        if (rd.GetString(0).IndexOf('-') > -1)
                        {
                            Users usr = new Users();
                            usr.ID = rd.GetString(0).Split('-')[1];
                            usr.Adi = RemoveTurkishCharacter(rd.GetString(1).ToUpper());

                            usr.Chz1 = KayitDurumu.Eklenecek;
                            usr.Chz2 = KayitDurumu.Eklenecek;
                            usr.Chz2 = KayitDurumu.Eklenecek;
                            usr.Chz2 = KayitDurumu.Eklenecek;

                            aktifUyeler.Add(usr);
                        }
                    }
                }
                cn.Close();
            }

            foreach (Users item in aktifUyeler)
            {
                if (!Program.Db.Uyeler.Contains(item))
                {
                    Program.Db.Uyeler.Add(item);
                }
            }

            foreach (Users item in Program.Db.Uyeler)
            {
                if (!item.AktifMi())
                {
                    if (aktifUyeler.Contains(item))
                    {
                        item.Chz1 = KayitDurumu.Eklenecek;
                        item.Chz2 = KayitDurumu.Eklenecek;
                        item.Chz2 = KayitDurumu.Eklenecek;
                        item.Chz2 = KayitDurumu.Eklenecek;
                    }
                }

                if (!aktifUyeler.Contains(item))
                {
                    if (item.Chz1 != KayitDurumu.Silindi)
                    {
                        item.Chz1 = KayitDurumu.Silinecek;
                    }

                    if (item.Chz2 != KayitDurumu.Silindi)
                    {
                        item.Chz2 = KayitDurumu.Silinecek;
                    }

                    if (item.Chz3 != KayitDurumu.Silindi)
                    {
                        item.Chz3 = KayitDurumu.Silinecek;
                    }

                    if (item.Chz4 != KayitDurumu.Silindi)
                    {
                        item.Chz4 = KayitDurumu.Silinecek;
                    }
                }
            }

        }
    }

    public class Database
    {
        List<Cihaz> _cihazlar = new List<Cihaz>();
        public List<Cihaz> Cihazlar { get { return _cihazlar; } }

        List<Users> _uyeler = new List<Users>();
        public List<Users> Uyeler { get { return _uyeler; } }
    }
}
